package com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.factory.WorkoutFactory;
import com.beerandjesus.sportofkings.handicapper.model.Workout;
import com.beerandjesus.sportofkings.handicapper.model.WorkoutImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WorkoutFactoryImpl extends AbstractObjectFactoryImpl implements WorkoutFactory {

    // Order of workout stats in PP data file
    private static final int DATE = 0;
    private static final int TIME = 1;
    private static final int TRACK = 2;
    private static final int DISTANCE = 3;
    private static final int CONDITION = 4;
    private static final int DESC = 5;
    private static final int TRACKIND = 6;
    private static final int NUMWORKS = 7;
    private static final int RANK = 8;

    /* (non-Javadoc)
     * @see com.beerandjesus.sportofkings.handicapper.factory.WorkoutFactory#getWorkouts(java.lang.String)
     */
    public List<Workout> getWorkouts(String line) {
        String[] values = getValues(line);
        return getWorkouts(values);
    }

    public List<Workout> getWorkouts(String[] values) {
        List<Workout> list = new ArrayList<Workout>(12);
        // Workouts start with date at index 102,
        // list dates in order for each workout,
        // then move onto listing all 12 times, etc.
        int idx = 102;
        int statCount = 0;
        for (int i = 0; i < 12; i++) {
            WorkoutImpl w;
            if (idx < 114) {
                w = new WorkoutImpl();
            } else {
                w = (WorkoutImpl) list.get(i);
            }
            switch (statCount) {
                case DATE:
                    w.setDate(val(values, idx));
                    break;
                case TIME:
                    w.setTime(val(values, idx));
                    break;
                case TRACK:
                    w.setTrack(val(values, idx));
                    break;
                case DISTANCE:
                    w.setDistance(val(values, idx));
                    break;
                case CONDITION:
                    w.setTrackCondition(val(values, idx));
                    break;
                case DESC:
                    w.setDescription(val(values, idx));
                    break;
                case TRACKIND:
                    w.setTrackIndicator(val(values, idx));
                    break;
                case NUMWORKS:
                    w.setNumWorksAtDistance(val(values, idx));
                    break;
                case RANK:
                    w.setRank(val(values, idx));
                    break;
            }
            if (idx < 114) {
                list.add(w);
            }
            if (idx == 209) break;
            idx++;
            if (i == 11) {
                i = -1;
                statCount++;
            }
        }

        return list;
    }

}
